package cloudUser.config;

import cloudEntity.entity.SmartUserEntity;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @program: Smart School
 * @ClassName UserDetailService
 * @description: 自定义 UserDetailService 实现用户信息获取。
 * @author: 陈智博
 * @create: 2025-03-27 20:21
 * @Version 1.0
 **/
@Getter
@Setter
@ToString
public class CustomerUserDetailsService implements UserDetails {

    private SmartUserEntity smartUserEntity;

    private List<String> permission;

    public CustomerUserDetailsService(SmartUserEntity smartUserEntity, List<String> authorities) {
        this.smartUserEntity = smartUserEntity;
        this.permission = authorities;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //将 角色 添加 到 授予的权限 合集 中
        return AuthorityUtils.createAuthorityList(permission);
    }

    @Override
    public String getPassword() {
        return this.smartUserEntity.getSysUserPassword();
    }

    @Override
    public String getUsername() {
        return this.smartUserEntity.getSysUserUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
